/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.start.controller;

import com.itgrail.pilot.app.command.cmo.*;
import com.itgrail.pilot.app.command.cmo.query.PageQueryMetadataCmd;
import com.itgrail.pilot.app.command.co.DictTypeListCO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itgrail.grail.cqrs.command.CommandBus;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;

/**
 * 元数据Controller
 * @author xujin
 */
@RestController
@Api("元数据管理")
@RequestMapping("/admin/metadata")
public class MetaDataController {

    @Autowired
    private CommandBus commandBus;

    @PostMapping("/page")
    @ApiOperation(value = "分页查询元数据")
    @RequiresPermissions("metadata:query")
    ResultData<PageResult<DictTypeListCO>> queryDictByPage(@RequestBody PageQueryMetadataCmd cmd) {
        return commandBus.send(cmd);

    }

    /**
     * 增加数据字典类型
     *
     * @param cmd
     * @return
     */
    @PostMapping("/addDictType")
    @ApiOperation(value = "增加数据字典类型")
    @RequiresPermissions("metadata:dictType:add")
    public ResultData addDictType(@RequestBody AddDictTypeCmd cmd) {
        return commandBus.send(cmd);
    }

    /**
     * 更新数据字典类型
     *
     * @param cmd
     * @return
     */
    @PostMapping("/updateDictType")
    @ApiOperation(value = "修改数据字典类型")
    @RequiresPermissions("metadata:dictType:update")
    public ResultData updateDictType(@RequestBody UpdateDictTypeCmd cmd) {
        return commandBus.send(cmd);
    }

    /**
     * 删除数据字典类型
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除数据字典类型")
    @RequiresPermissions("metadata:dictType:update")
    @GetMapping("/deleteDictType/{id}")
    public ResultData<Void> deleteDictType(@PathVariable Long id) {
        DeleteDictTypeCmd cmd = new DeleteDictTypeCmd();
        cmd.setId(id);
        return commandBus.send(cmd);

    }

    /**
     * 增加数据字典项
     *
     * @param cmd
     * @return
     */
    @ApiOperation(value = "增加数据字典项")
    @RequiresPermissions("metadata:dictData:add")
    @PostMapping("/addDictData")
    public ResultData addDictData(@RequestBody AddDictDataCmd cmd) {
        return commandBus.send(cmd);
    }

    /**
     * 更新数据字典项
     *
     * @param cmd
     * @return
     */
    @ApiOperation(value = "更新数据字典项")
    @RequiresPermissions("metadata:dictData:update")
    @PostMapping("/updateDictData")
    public ResultData updateDictData(@RequestBody UpdateDictDataCmd cmd) {
        return commandBus.send(cmd);
    }

    /**
     * 删除数据字典项
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除数据字典项")
    @RequiresPermissions("metadata:dictData:delete")
    @GetMapping("/deleteDictData/{id}")
    public ResultData deleteDictData(@PathVariable Long id) {
        DeleteDictDataCmd cmd = new DeleteDictDataCmd();
        cmd.setId(id);
        return commandBus.send(cmd);
    }


}
